﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmVPurcheaseOrder : Form
    {
        DataTable LvTable2 = new DataTable("Details");
        public frmVPurcheaseOrder()
        {
            InitializeComponent();
        }

        private void frmVPurcheaseOrder_Load(object sender, EventArgs e)
        {

            FillVendorDropDown();

        }


        private void VendorPOFillGrid()
        {
            //Fill the DataGrid
            List<Purchase_Order> LvList = Purchase_OrderDAO.GetAllPos();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvPO.DataSource = LvTable;
            LvTable.Columns.Add("Vendor_ID ");
            LvTable.Columns.Add("PONo ");
            LvTable.Columns.Add("PRNo ");
            LvTable.Columns.Add("PoDate ");
            LvTable.Columns.Add("BranchId ");
            

            foreach (Purchase_Order LvPO in LvList)
            {
                LvTable = (DataTable)dgvPO.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPO.Sup_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPO.Po_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPO.Pr_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvPO.Po_Date.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvPO.Branch_Id.ToString();
                


            }

        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            VendorPOFillGrid();
        }


        private void FillVendorDropDown()
        {
            //Fill the DataGrid
            List<Supplier> LvList = SupplierDAO.GetAllSupplier();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            //cmbCategory.DataSource = LvTable;
            LvTable.Columns.Add("SupID");
            LvTable.Columns.Add("SupName");

            string lvtest = "";

            foreach (Supplier LvSup in LvList)
            {
                //LvTable = (DataTable)cmbCategory.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                lvtest = LvSup.Sup_ID.ToString() + '-' + LvSup.Sup_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][0] = lvtest;
                //LvTable.Rows[LvTable.Rows.Count - 1][0] = LvCategory.Cat_ID.ToString() +'-' + LvCategory.Cat_Name.ToString();
                //LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSubCategory.Subcat_ID.ToString();


            }

            cmbVendorId.DisplayMember = "SupID";
            cmbVendorId.ValueMember = "SupID";
            cmbVendorId.DataSource = LvTable;
            //cmbCategory.DataBindings();



        }

        private void VPOsFillGridByVendorId()
        {
            //Fill the DataGrid
            List<Purchase_Order> LvList = Purchase_OrderDAO.GetPoByVId(cmbVendorId.Text.ToString().Substring(0,8));
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvPO.DataSource = LvTable;
            LvTable.Columns.Add("Vendor_ID ");
            LvTable.Columns.Add("PONo ");
            LvTable.Columns.Add("PRNo ");
            LvTable.Columns.Add("PoDate ");
            LvTable.Columns.Add("BranchId ");


            foreach (Purchase_Order LvPO in LvList)
            {
                LvTable = (DataTable)dgvPO.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());
                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPO.Sup_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPO.Po_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPO.Pr_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvPO.Po_Date.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvPO.Branch_Id.ToString();


            }

        }

        private void button66_Click(object sender, EventArgs e)
        {
            VPOsFillGridByVendorId();
        }


        private void PoItemFillGrid()
        {

            //Fill the DataGrid


            



            LvTable2.Columns.Add("PONo ");
            LvTable2.Columns.Add("PLId ");
            LvTable2.Columns.Add("PurchasePrice ");
            LvTable2.Columns.Add("ProductId ");
            LvTable2.Columns.Add("ProductName ");
            LvTable2.Columns.Add("Qty ");
            Purchase_Order LvPo = new Purchase_Order();

            LvPo.GvPo_Id = dgvPO.Rows[dgvPO.SelectedCells[0].RowIndex].Cells[1].Value.ToString();
            
            //GvPONo = LvPO.pono;
            LvTable2 = Purchase_OrderDAO.GetPoDetailsByPoId(LvPo.GvPo_Id);
            dgvItemdetails.DataSource = LvTable2;

            //DataTable LvTable2 = new DataTable();


            //foreach (Purchase_Order LvPoItems in LvList)
            //{
            //    LvTable2 = (DataTable)dgvItemdetails.DataSource;

            //    LvTable2.Rows.Add(LvTable2.NewRow());

            //    LvTable2.Rows[LvTable2.Rows.Count - 1][0] = LvPoItems.grnno.ToString();
            //    LvTable2.Rows[LvTable2.Rows.Count - 1][1] = LvPoItems.qty.ToString();
            //    LvTable2.Rows[LvTable2.Rows.Count - 1][2] = LvPoItems.unit.ToString();
            //    LvTable2.Rows[LvTable2.Rows.Count - 1][3] = LvPoItems.rate.ToString();
            //    LvTable2.Rows[LvTable2.Rows.Count - 1][4] = LvPoItems.itemcode.ToString();



            //}
        }

        private void dgvItemdetails_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //PoItemFillGrid();
        }

        private void dgvPO_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            PoItemFillGrid();
        }
    }
}
